package com.rika.controller;

import com.rika.pojo.CheckStock;
import com.rika.pojo.Customer;
import com.rika.pojo.Product;
import com.rika.pojo.StockRecord;
import com.rika.response.Result;
import com.rika.service.CheckStockService;
import com.rika.service.ProductService;
import com.rika.service.StockRecordService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CheckStockController {
    @Resource
    private CheckStockService checkStockService;
    @Resource
    private ProductService productService;
    @Resource
    private StockRecordService stockRecordService;

    @RequestMapping("addCheckStock")
    public Result addCheckStock(CheckStock checkStock,Integer changeNum){
        Result result=new Result();
        List<Product> list=productService.selectByProductCode(checkStock.getProductCode());
        StockRecord stockRecord=new StockRecord();
        stockRecord.setProductCode(checkStock.getProductCode());
        stockRecord.setStockNum(changeNum);
        stockRecord.setCreateUser(checkStock.getCreateUser());
        stockRecord.setStockTime(checkStock.getStockTime());
        Product product=new Product();
        product.setProductCode(checkStock.getProductCode());
        if(checkStock.getType().equals("损耗")){
            checkStock.setOriginNum(list.get(0).getNum());
            checkStock.setRealNum(list.get(0).getNum()-changeNum);
            product.setNum(list.get(0).getNum()-changeNum);
            stockRecord.setStockType(1);
        }
        if(checkStock.getType().equals("盈余")){
            checkStock.setOriginNum(list.get(0).getNum());
            checkStock.setRealNum(list.get(0).getNum()+changeNum);
            product.setNum(list.get(0).getNum()+changeNum);
            stockRecord.setStockType(3);
        }
        productService.updateProduct(product);
        checkStockService.addCheckStock(checkStock);
        stockRecordService.addStockRecord(stockRecord);
        result.setMsg("新增成功");
        result.setCode(200);
        return result;
    }
    @RequestMapping("selectInCheckStock")
    public Result selectInCheckStock(String productCode){
        Result result=new Result();
        List<CheckStock> list=checkStockService.selectInCheckStock(productCode);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("selectOutCheckStock")
    public Result selectOutCheckStock(String productCode){
        Result result=new Result();
        List<CheckStock> list=checkStockService.selectOutCheckStock(productCode);
        result.setCode(200);
        result.setData(list);
        return result;
    }
}

